package com.qike.duola.controller;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.qike.duola.dto.*;
import com.qike.duola.response.BaseResult;
import com.qike.duola.response.RetResponse;
import com.qike.duola.service.IsysResourceService;
import com.qike.duola.service.IsysRoleService;
import com.qike.duola.service.IsysUserService;
import com.qike.duola.utils.TokenUtil;
import com.qike.duola.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(description = "角色控制器")
@RestController
@RequestMapping("/role")
@CrossOrigin
public class RoleController {

    private static Logger logger = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private TokenUtil tokenUtil;

    @Autowired
    private DefaultKaptcha captchaProducer;
    @Autowired
    private IsysUserService userService;
    @Autowired
    private IsysResourceService isysResourceService;

    @Autowired
    private IsysRoleService isysRoleService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * 角色列表
     *
     * @return
     */
    @GetMapping("/roleList")
    @ApiOperation("角色列表")
    @ResponseBody
//    @RequiresPermissions("system:role:*")
    public BaseResult roleList(ResourceListDto resourceListDto) {
        try {
            RoleListVo roleList = isysRoleService.roleList(resourceListDto);
            return RetResponse.makeOKRsp(roleList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 角色下拉列表
     *
     * @return
     */
    @GetMapping("/rolePullList")
    @ApiOperation("角色下拉列表")
    @ResponseBody
//    @RequiresPermissions("system:role:*")
    public BaseResult rolePullList(String name) {
        try {
            List<RolePullListVo> roleList = isysRoleService.rolePullList(name);
            return RetResponse.makeOKRsp(roleList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


    /**
     * 删除角色
     *
     * @return
     */
    @PostMapping("/roleDel")
    @ApiOperation("删除角色")
    @ResponseBody
//    @RequiresPermissions("system:role:del")
    public BaseResult roleDel(@RequestBody RoleDelDto roleDelDto) {
        try {
            isysRoleService.roleDel(roleDelDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 添加角色
     *
     * @return
     */
    @PostMapping("/roleCreate")
    @ApiOperation("添加角色")
    @ResponseBody
//    @RequiresPermissions("system:role:create")
    public BaseResult roleCreate(@RequestBody RoleCreateDto roleCreateDto) {
        try {
            isysRoleService.roleCreate(roleCreateDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 编辑角色
     *
     * @return
     */
    @PostMapping("/roleEdit")
    @ApiOperation("编辑角色")
    @ResponseBody
//    @RequiresPermissions("system:role:edit")
    public BaseResult roleEdit(@RequestBody RoleEditDto roleEditDto) {
        try {
            isysRoleService.roleEdit(roleEditDto);
            return RetResponse.makeOKRsp();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }

    /**
     * 角色详情
     *
     * @return
     */
    @GetMapping("/roleInfo")
    @ApiOperation("角色详情")
    @ResponseBody
//    @RequiresPermissions("system:role:info")
    public BaseResult roleInfo(RoleInfoDto roleInfoDto) {
        try {
            RoleInfoVo vo = isysRoleService.roleInfo(roleInfoDto);
            return RetResponse.makeOKRsp(vo);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return RetResponse.makeErrRsp(e.getMessage());
        }
    }


}
